<template>
  <el-dialog
    v-dialogDrag
    :visible="popupsVisible"
    width="70%"
    :append-to-body="true"
    :close-on-click-modal="false"
    @close="closeEdit"
    @open="open"
  >
    <dialogHeader slot="title" :title="titleName" :style-type="$store.state.app.dialogType" />
    <!-- 显示按钮的onetabel -->
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-more-btn="false"
      :callback-obj="callbackObj"
      :dynamic-is-show-select="false"
    />
    <edit

      :cur-popups-visible="editPopupsVisible"
      :type="type"
      :edit-row-data11="editRowData1"
      @curPopupsVisible="toCloseDialog"
    />
  </el-dialog>
</template>

<script>
import dialogHeader from '@/components/dialogHeader'
import OneTableTemplate from '@/components/templates/oneTable'
import { orgApis } from '@/api/paSparePartsManagement'
// import { orgApis } from '@/api/vesalesapi.js'
export default {
  components: {
    dialogHeader,
    OneTableTemplate,
    Edit: () => import('./edit')
  },
  props: {
    popupsVisible: {
      type: Boolean,
      default: false
    },
    dataForForm: {
      type: Object,
      default: () => {}
    },
    editRowData: {
      type: Object
    }
  },
  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      editRowData1: {},
      type: '',
      selectiondata: [],
      editPopupsVisible: false,
      titleName: '索赔申请单审核',
      // 网格查询API配置对象
      apiConfig: {
        ServiceCode: 'queryBigArea',
        APIUrl: '/mp/e3sh/basics/ve/bigareaService/queryBigArea.do'
      },
      // 动态组件-按钮
      tableButtons:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableButtons &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableButtons
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableButtons
          : [
            {
              compKey: 'btnKey1',
              type: 'primary',
              size: 'small',
              name: '',
              clickEvent: () => this.showEdit('1'),
              text: '通过'
            },
            {
              compKey: 'btnKey2',
              type: '',
              size: 'small',
              icon: 'iconfont  icon-cancel',
              clickEvent: () => this.showEdit('2'),
              text: '驳回'
            },
            {
              compKey: 'btnKey1',
              type: 'primary',
              size: 'small',
              name: '',
              clickEvent: () => {
                const { annex } = this.editRowData
                const loading = this.$loading({ text: `请稍等...` })
                this.$requestAPI({
                  method: 'GET',
                  apiUrl: annex,
                  variables: {dataType: 'excel'}
                }).then(res => {
                  this.$utils.downloadFile(res, `附件.xlsx`)
                }).catch(err => {
                  if (err.response.data.msg) {
                    this.$message(err.response.data.msg)
                  } else {
                    this.$message(err.message)
                  }
                }).finally(() => {
                  loading.close()
                })
              },
              text: '查看附件',
              position: 'left'
            }
          ],
      // 动态组件-查询条件
      tableComponents: this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
      this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableComponents.length > 0
        ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableComponents : [
          {
            compKey: 'compKey1',
            labelName: '索赔类型：',
            codeField: 'claimOrderTypeCn',
            component: () => import('@/components/org/commonInput'),
            type: 'inputText',
            isMust: true,
            disabled: true,
            span: 6
          },
          {
            compKey: 'compKey2',
            labelName: '申请单号：',
            codeField: 'claimOrderCode',
            component: () => import('@/components/org/commonInput'),
            type: 'inputText',
            isMust: true,
            disabled: true,
            span: 6
          },
          {
            compKey: 'compKey3',
            labelName: '关联单号：',
            codeField: 'purOrderCode',
            component: () => import('@/components/org/commonInput'),
            type: 'inputText',
            isMust: true,
            disabled: true,
            span: 6
          },
          {
            compKey: 'compKey4',
            labelName: '索赔总金额：',
            codeField: 'claimAmount',
            component: () => import('@/components/org/commonInput'),
            type: 'inputText',
            isMust: true,
            disabled: true,
            span: 6
          },
          {
            compKey: 'compKey5',
            labelName: '索赔原因：',
            codeField: 'claimReason',
            component: () => import('@/components/org/commonInput'),
            type: 'inputText',
            isMust: true,
            disabled: true,
            span: 6
          },
          {
            compKey: 'compKey6',
            labelName: '责任方：',
            codeField: 'responsiblePartyCn',
            component: () => import('@/components/org/commonInput'),
            type: 'inputText',
            isMust: true,
            disabled: true,
            span: 6
          },
          {
            compKey: 'compKey7',
            labelName: '网点编码：',
            codeField: 'dlrCode',
            component: () => import('@/components/org/commonInput'),
            type: 'inputText',
            isMust: true,
            disabled: true,
            span: 6
          },
          {
            compKey: 'compKey8',
            labelName: '网点名称：',
            codeField: 'dlrShortName',
            component: () => import('@/components/org/commonInput'),
            type: 'inputText',
            isMust: true,
            disabled: true,
            span: 6
          }
        ],

      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'releaseId',
              label: '主键ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'bigAreaId',
              label: '大区ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'partNo',
              label: '备件编码'
            },
            {
              prop: 'partName',
              label: '备件名称'
            },

            {
              prop: 'oemOutQty',
              label: '总部出库数量'
            },
            {
              prop: 'dlrInQty',
              label: '网点入库数量'
            },
            {
              prop: 'claimQty',
              label: '索赔申请数量'
            },
            {
              prop: 'claimDReason',
              label: '索赔原因'
            },
            {
              prop: 'claimPrice',
              label: '索赔单价'
            },
            {
              prop: 'claimAmount',
              label: '索赔金额'
            }
          ],
      formField: {
        dlrShortName: '',
        dlrCode: '',
        responsibleParty: '',
        claimReason: '',
        purOrderCode: '',
        claimOrderCode: '',
        claimOrderType: '',
        claimAmount: ''

      },
      callbackObj: {
        // 手动勾选时
        onCheckSelect: (selection, row) => {
          this.selectiondata = selection
          console.log(selection)
        },
        onCheckSelectChange: (selection) => {
          this.selectiondata = selection
          console.log(selection)
        }

      }
    }
  },
  watch: {
    popupsVisible(newlo, oldV) {
      if (newlo) {
        this.editRowData1 = this.editRowData
      }
    }
  },
  // mounted() {
  //   this.editRowData1 = this.editRowData
  //   // this.$nextTick(() => {
  //   //   this.queryform()
  //   // })
  // },
  methods: {

    toCloseDialog() {
      this.editPopupsVisible = false
    },
    // //通过
    // adopt(){
    //     if(this.selectiondata.length<1) return this.$message('请至少勾选一条数据！')

    //   this.passOrReject(orgApis.purchaseClaimReviewPass)
    // },
    // //驳回
    // reject(){
    //     if(this.selectiondata.length<1) return this.$message('请至少勾选一条数据！')
    //    this.passOrReject(orgApis.purchaseClaimReviewReject)
    // },
    showEdit(type) {
      if (type == '1') {
        // 通过
        this.type = '1'
      } else if (type == '2') {
        this.type = '2'
      }
      // console.log(this.editPopupsVisible)
      this.editPopupsVisible = true
    },

    // 网格查询
    passOrReject(api) {
      const that = this
      // let obj = {
      //   data:data
      // }
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: api,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: this.formField

      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then(response => {
        if (response.result === '1') {
          this.$message({
            message: response.msg, /* 保存成功*/
            type: 'success',
            duration: 1000
          })

          this.closeEdit()
          this.$parent.queryTable(1)

          // that.$utils.deepCopy(that.$refs["multipleTable"].list) = response.rows
          //  this.$utils.deepCopy(this.$refs["multipleTable"].list) = response.rows

          //  this.$refs["multipleTable"].list = response.rows
        } else {
          // 保存失败
          this.$message({
            message:
             response.msg,
            type: 'warn',
            uration: 1000
          })
        }
      })
    },

    // 关闭弹窗
    closeEdit(type) {
      this.$emit('close', type)
    },

    open() {
      console.log(this.editRowData)
      this.formField = this.editRowData
      this.search()
    },

    // 网格查询
    search() {
      const that = this

      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: orgApis.purchaseClaimReviewTableQuery,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: this.formField

      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then(response => {
        if (response.result === '1') {
          this.$message({
            message: response.msg, /* 保存成功*/
            type: 'success',
            duration: 1000
          })

          // that.$utils.deepCopy(that.$refs["multipleTable"].list) = response.rows
          //  this.$utils.deepCopy(this.$refs["multipleTable"].list) = response.rows

          this.$refs['multipleTable'].list = response.rows
        } else {
          // 保存失败
          this.$message({
            message:
             response.msg,
            type: 'warn',
            uration: 1000
          })
        }
      })
    }
    // queryform() {
    // var queryid = { orderId: this.dataForForm.saleOrderId }
    // // 先读取参数中的传输对象
    // const returnObj = {
    // // api配置
    //   apiConfig: orgApis.orderinvoicedetailsQuery,
    // 条件/实体参数（变量），根据typeParam中的定义配置
    // variables: queryid
    // }
    // 转换了中台请求格式数据
    // var paramD = this.$getParams(returnObj, null, this.dynamicApiStyle)
    // 调用中台API方法（可复用）
    // this.$requestAPI(paramD, this.dynamicApiStyle).then(response => {
    //   if (response.result === '0') {
    //     if (response.msg) {
    //       this.$message({
    //         message: response.msg,
    //         type: 'warn',
    //         uration: 2000
    //       })
    //     }
    //     return
    //   }
    //   // 返回json
    //   if (response.result === '1') {
    //     Object.assign(this.formField, response.rows[0], response.rows[1])
    //     console.log(this.formField)
    //   }
    // }).catch(err => {
    //   console.info(err)
    // })
    // }
  }
}
</script>
<style lang='scss' scoped>
  /deep/ .el-dialog{
    margin-top:5rem !important
  }
</style>
